Completed
Pull Request — master (#1653)
by Aristeides
04:39 queued 02:31
created

wp.customize.Control.extend.initKirkiControl   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43

Duplication

Lines 43
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 43
loc 43
rs 8.8571
c 0
b 0
f 0
1
/* global kirkiControlLoader */
2 View Code Duplication
wp.customize.controlConstructor['kirki-background'] = wp.customize.Control.extend({
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3
4
	// When we're finished loading continue processing
5
	ready: function() {
6
7
		'use strict';
8
9
		var control = this;
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
10
11
		// Init the control.
12
		if ( ! _.isUndefined( window.kirkiControlLoader ) && _.isFunction( kirkiControlLoader ) ) {
13
			kirkiControlLoader( control );
14
		} else {
15
			control.initKirkiControl();
16
		}
17
	},
18
19
	initKirkiControl: function() {
20
21
		var control = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
22
		    value   = control.getValue(),
23
		    picker  = control.container.find( '.kirki-color-control' );
24
25
		// Hide unnecessary controls if the value doesn't have an image.
26
		if ( _.isUndefined( value['background-image'] ) || '' === value['background-image'] ) {
27
			control.container.find( '.background-wrapper > .background-repeat' ).hide();
28
			control.container.find( '.background-wrapper > .background-position' ).hide();
29
			control.container.find( '.background-wrapper > .background-size' ).hide();
30
			control.container.find( '.background-wrapper > .background-attachment' ).hide();
31
		}
32
33
		// Color.
34
		picker.wpColorPicker({
35
			change: function() {
36
				setTimeout( function() {
37
					control.saveValue( 'background-color', picker.val() );
38
				}, 100 );
39
			}
40
		});
41
42
		// Background-Repeat.
43
		control.container.on( 'change', '.background-repeat select', function() {
44
			control.saveValue( 'background-repeat', jQuery( this ).val() );
45
		});
46
47
		// Background-Size.
48
		control.container.on( 'change click', '.background-size input', function() {
49
			control.saveValue( 'background-size', jQuery( this ).val() );
50
		});
51
52
		// Background-Position.
53
		control.container.on( 'change', '.background-position select', function() {
54
			control.saveValue( 'background-position', jQuery( this ).val() );
55
		});
56
57
		// Background-Attachment.
58
		control.container.on( 'change click', '.background-attachment input', function() {
59
			control.saveValue( 'background-attachment', jQuery( this ).val() );
60
		});
61
62
		// Background-Image.
63
		control.container.on( 'click', '.background-image-upload-button', function( e ) {
64
			var image = wp.media({ multiple: false }).open().on( 'select', function() {
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
65
66
				// This will return the selected image from the Media Uploader, the result is an object.
67
				var uploadedImage = image.state().get( 'selection' ).first(),
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
68
				    previewImage   = uploadedImage.toJSON().sizes.full.url,
69
				    imageUrl,
70
				    imageID,
71
				    imageWidth,
72
				    imageHeight,
73
				    preview,
74
				    removeButton;
75
76
				if ( ! _.isUndefined( uploadedImage.toJSON().sizes.medium ) ) {
77
					previewImage = uploadedImage.toJSON().sizes.medium.url;
78
				} else if ( ! _.isUndefined( uploadedImage.toJSON().sizes.thumbnail ) ) {
79
					previewImage = uploadedImage.toJSON().sizes.thumbnail.url;
80
				}
81
82
				imageUrl    = uploadedImage.toJSON().sizes.full.url;
83
				imageID     = uploadedImage.toJSON().id;
0 ignored issues
show
Unused Code introduced by
The variable imageID seems to be never used. Consider removing it.
Loading history...
84
				imageWidth  = uploadedImage.toJSON().width;
0 ignored issues
show
Unused Code introduced by
The variable imageWidth seems to be never used. Consider removing it.
Loading history...
85
				imageHeight = uploadedImage.toJSON().height;
0 ignored issues
show
Unused Code introduced by
The variable imageHeight seems to be never used. Consider removing it.
Loading history...
86
87
				// Show extra controls if the value has an image.
88
				if ( '' !== imageUrl ) {
89
					control.container.find( '.background-wrapper > .background-repeat, .background-wrapper > .background-position, .background-wrapper > .background-size, .background-wrapper > .background-attachment' ).show();
90
				}
91
92
				control.saveValue( 'background-image', imageUrl );
93
				preview      = control.container.find( '.placeholder, .thumbnail' );
94
				removeButton = control.container.find( '.background-image-upload-remove-button' );
95
96
				if ( preview.length ) {
97
					preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '<img src="' + previewImage + '" alt="" />' );
98
				}
99
				if ( removeButton.length ) {
100
					removeButton.show();
101
				}
102
		    });
103
104
			e.preventDefault();
105
		});
106
107
		control.container.on( 'click', '.background-image-upload-remove-button', function( e ) {
108
109
			var preview,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
110
			    removeButton;
111
112
			e.preventDefault();
113
114
			control.saveValue( 'background-image', '' );
115
116
			preview      = control.container.find( '.placeholder, .thumbnail' );
117
			removeButton = control.container.find( '.background-image-upload-remove-button' );
118
119
			// Hide unnecessary controls.
120
			control.container.find( '.background-wrapper > .background-repeat' ).hide();
121
			control.container.find( '.background-wrapper > .background-position' ).hide();
122
			control.container.find( '.background-wrapper > .background-size' ).hide();
123
			control.container.find( '.background-wrapper > .background-attachment' ).hide();
124
125
			if ( preview.length ) {
126
				preview.removeClass().addClass( 'placeholder' ).html( 'No file selected' );
127
			}
128
			if ( removeButton.length ) {
129
				removeButton.hide();
130
			}
131
		});
132
	},
133
134
	/**
135
	 * Gets the value.
136
	 */
137
	getValue: function() {
138
139
		var control = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
140
		    value   = {};
141
142
		// Make sure everything we're going to need exists.
143
		_.each( control.params['default'], function( defaultParamValue, param ) {
144
			if ( false !== defaultParamValue ) {
145
				value[ param ] = defaultParamValue;
146
				if ( ! _.isUndefined( control.setting._value[ param ] ) ) {
147
					value[ param ] = control.setting._value[ param ];
148
				}
149
			}
150
		});
151
		_.each( control.setting._value, function( subValue, param ) {
152
			if ( _.isUndefined( value[ param ] ) ) {
153
				value[ param ] = subValue;
154
			}
155
		});
156
		return value;
157
	},
158
159
	/**
160
	 * Saves the value.
161
	 */
162
	saveValue: function( property, value ) {
163
164
		'use strict';
165
166
		var control   = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
167
		    input     = jQuery( '#customize-control-' + control.id.replace( '[', '-' ).replace( ']', '' ) + ' .background-hidden-value' ),
168
		    valueJSON = jQuery( input ).val(),
169
		    valueObj  = JSON.parse( valueJSON );
170
171
		valueObj[ property ] = value;
172
		control.setting.set( valueObj );
173
		jQuery( input ).attr( 'value', JSON.stringify( valueObj ) ).trigger( 'change' );
174
	}
175
});
176